package com.wyzz.hr.action;

import java.util.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IEmpsalaryperiodBo;
import com.wyzz.hr.bo.ISalaryPaidBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.pojo.Empsalaryperiod;
import com.wyzz.hr.spring.SpringBeanFactory;

public class EmailSendForSalarypay extends BaseAction {

	public EmailSendForSalarypay() {
	}

	public List sendEmail(String emps[], String yearMonth) {
		List err = new ArrayList();
		String flt = DWRUtil.checkAuth("salarypayEmailSend", "sendEmail");
		if ("error".equalsIgnoreCase(flt)) {
			err.add("\u60A8\u65E0\u6743\u53D1\u9001\u90AE\u4EF6\uFF01");// 您无权发送邮件
																		// ！
			return err;
		}
		IEmpsalaryperiodBo salaryperiod = (IEmpsalaryperiodBo) SpringBeanFactory
				.getBean("empsalaryperiodBO");
		Empsalaryperiod period = salaryperiod.loadEspdStatus(yearMonth);
		if (period == null || period.getEspdStatus().intValue() != 2) {
			err
					.add((new StringBuilder())
							.append(yearMonth)
							.append(
									"\u85AA\u8D44\u672A\u5BA1\u6838\u5B8C\u6BD5\uFF0C\u4E0D\u80FD\u53D1\u9001\u85AA\u8D44\u90AE\u4EF6\uFF01")// 薪资未审核完毕
																																				// ，
																																				// 不能发送薪资邮件
																																				// ！
							.toString());
			return err;
		}
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		List result = salaryPaidBo.getDecriedSalaryPaidForEmailSend(emps,
				yearMonth);
		List noPayEmp = new ArrayList();
		for (int i = 0; i < emps.length; i++) {
			boolean flag = false;
			Iterator it = result.iterator();
			do {
				if (!it.hasNext())
					break;
				Empsalarypay pay = (Empsalarypay) it.next();
				if (!emps[i].equals(pay.getEspEmpno().getId()))
					continue;
				flag = true;
				break;
			} while (true);
			if (!flag)
				noPayEmp.add(emps[i]);
		}

		if (!noPayEmp.isEmpty()) {
			IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
			int size = noPayEmp.size();
			String arr[] = new String[size];
			for (int i = 0; i < size; i++)
				arr[i] = (String) noPayEmp.get(i);

			List employees = empBo.searchEmpArray(arr);
			String er = "";
			for (Iterator it = employees.iterator(); it.hasNext();) {
				Employee e = (Employee) it.next();
				er = (new StringBuilder()).append(er).append(e.getEmpName())
						.append("\uFF0C").toString();
			}

			err.add((new StringBuilder()).append(
					er.substring(0, er.length() - 1)).append(
					"\u5F53\u6708\u65E0\u85AA\u8D44\u6570\u636E\uFF01")
					.toString());
		}
		err.addAll(salaryPaidBo.getEmailList(result));
		return err;
	}

	private static final long serialVersionUID = 1L;
}
